package com.itheima.tliasmanagement.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@RequestMapping("/session")
@RestController
public class SessionController {

    @GetMapping("/login")
    public String login(HttpServletResponse response, HttpServletRequest request){
        // 用户名 密码校验
        // 生成session
        HttpSession session = request.getSession();
        log.info("login, hashcode:{}", session.hashCode());
        session.setAttribute("userId", "1");
        session.setAttribute("username", "zhangsan");
        return "success";
    }

    /**
     * 获取用户的订单列表
     * */
    @GetMapping("orders")
    public String orders(HttpServletRequest request){
        // 从session中获取用户信息
        HttpSession session = request.getSession();
        log.info("orders, hashcode:{}", session.hashCode());
        String userId = (String)session.getAttribute("userId");
        String username = (String)session.getAttribute("username");
        // 根据用户id查询订单列表
        // select * from order where userId= #{userId}
        return userId + ":" + username + ":order list" ;
    }

}
